﻿using MvcApplication.Data.Mapping;
using MvcApplication.Entity;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace MvcApplication.Data
{
    public class MvcApplicationContext : DbContext
    {
        static MvcApplicationContext()
        {
            Database.SetInitializer<MvcApplicationContext>(null);
        }

        public MvcApplicationContext()
            : base("name=MvcApplicationContext")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
        }

        public MvcApplicationContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }

        public MvcApplicationContext(string nameOrConnectionString, DbCompiledModel model)
            : base(nameOrConnectionString, model)
        {
        }

        public MvcApplicationContext(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {
        }

        public MvcApplicationContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection)
            : base(existingConnection, model, contextOwnsConnection)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            #region 文章
            modelBuilder.Configurations.Add(new Post_Mapping());
            modelBuilder.Configurations.Add(new Category_Mapping());
            #endregion
            modelBuilder.Configurations.Add(new Picture_Mapping());
            modelBuilder.Configurations.Add(new PictureBank_Mapping());

            modelBuilder.Configurations.Add(new UserInfo_Mapping());
            modelBuilder.Configurations.Add(new Role_Mapping());
            modelBuilder.Configurations.Add(new RoleMenu_Mapping());
            modelBuilder.Configurations.Add(new DisplayInfo_Mapping());
            modelBuilder.Configurations.Add(new Option_Mapping());
        }

        #region 文章
        public DbSet<Post> Post { get; set; }
        public DbSet<DisplayInfo> DisplayInfo { get; set; }
        public DbSet<Category> Category { get; set; }
        //public DbSet<PostCategory> PostCategory { get; set; }
        #endregion

        public DbSet<Picture> Picture { get; set; }
        public DbSet<PictureBank> PictureBank { get; set; }


        public DbSet<UserInfo> UserInfo { get; set; }
        public DbSet<Option> Option { get; set; }

        #region 用户权限

        public DbSet<Role> Role { get; set; }
        public DbSet<RoleMenu> RoleMenu { get; set; }

        #endregion

        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
            catch (DBConcurrencyException ex)
            {
                throw ex;
            }
            catch (DbUpdateException ex)
            {
                throw ex;
            }
        }
    }
}
